package com.example.mydy.withdraw;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.UserInfo;
import com.example.mydy.model.WithdrawalRecord;
import com.example.mydy.utils.MockDataManager;

import java.util.ArrayList;
import java.util.List;

public class WithdrawalHistoryActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private LinearLayout emptyState;
    private Button btnGotoWithdraw;
    private ImageButton btnBack;
    
    private Button btnFilterAll;
    private Button btnFilterPending;
    private Button btnFilterApproved;
    private Button btnFilterRejected;
    
    private WithdrawalRecordAdapter adapter;
    private MockDataManager mockDataManager;
    private List<WithdrawalRecord> allRecords = new ArrayList<>();
    private String currentFilter = "all"; // 当前过滤条件：all, pending, approved, rejected

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdrawal_history);
        
        // 初始化视图
        initViews();
        
        // 初始化数据
        initData();
        
        // 设置点击事件
        setupListeners();
    }

    private void initViews() {
        recyclerView = findViewById(R.id.rv_withdrawal_records);
        emptyState = findViewById(R.id.empty_state);
        btnGotoWithdraw = findViewById(R.id.btn_goto_withdraw);
        btnBack = findViewById(R.id.btn_back);
        
        btnFilterAll = findViewById(R.id.btn_filter_all);
        btnFilterPending = findViewById(R.id.btn_filter_pending);
        btnFilterApproved = findViewById(R.id.btn_filter_approved);
        btnFilterRejected = findViewById(R.id.btn_filter_rejected);
        
        // 设置RecyclerView
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    private void initData() {
        // 获取MockDataManager实例
        mockDataManager = MockDataManager.getInstance();
        
        // 获取当前用户的提现记录
        UserInfo currentUser = mockDataManager.getCurrentUser();
        if (currentUser != null) {
            allRecords = mockDataManager.getUserWithdrawalRecords(currentUser.getUsername());
        }
        
        // 创建适配器
        adapter = new WithdrawalRecordAdapter(this, filterRecords(currentFilter));
        recyclerView.setAdapter(adapter);
        
        // 更新UI状态
        updateUIState();
    }

    private void setupListeners() {
        // 返回按钮
        btnBack.setOnClickListener(v -> finish());
        
        // 去提现按钮
        btnGotoWithdraw.setOnClickListener(v -> {
            Intent intent = new Intent(this, WithdrawActivity.class);
            startActivity(intent);
            finish();
        });
        
        // 过滤按钮
        btnFilterAll.setOnClickListener(v -> {
            setFilter("all");
        });
        
        btnFilterPending.setOnClickListener(v -> {
            setFilter(WithdrawalRecord.STATUS_PENDING);
        });
        
        btnFilterApproved.setOnClickListener(v -> {
            setFilter(WithdrawalRecord.STATUS_APPROVED);
        });
        
        btnFilterRejected.setOnClickListener(v -> {
            setFilter(WithdrawalRecord.STATUS_REJECTED);
        });
    }

    private void setFilter(String filter) {
        // 更新当前过滤条件
        currentFilter = filter;
        
        // 更新过滤按钮样式
        updateFilterButtons();
        
        // 更新记录列表
        adapter.updateData(filterRecords(filter));
        
        // 更新UI状态
        updateUIState();
    }

    private List<WithdrawalRecord> filterRecords(String filter) {
        if (filter.equals("all")) {
            return new ArrayList<>(allRecords);
        }
        
        List<WithdrawalRecord> filteredRecords = new ArrayList<>();
        for (WithdrawalRecord record : allRecords) {
            if (record.getStatus().equals(filter)) {
                filteredRecords.add(record);
            }
        }
        
        return filteredRecords;
    }

    private void updateFilterButtons() {
        // 重置所有按钮样式
        btnFilterAll.setBackgroundResource(R.drawable.button_outline);
        btnFilterAll.setTextColor(getResources().getColor(R.color.purple_500));
        
        btnFilterPending.setBackgroundResource(R.drawable.button_outline);
        btnFilterPending.setTextColor(getResources().getColor(R.color.purple_500));
        
        btnFilterApproved.setBackgroundResource(R.drawable.button_outline);
        btnFilterApproved.setTextColor(getResources().getColor(R.color.purple_500));
        
        btnFilterRejected.setBackgroundResource(R.drawable.button_outline);
        btnFilterRejected.setTextColor(getResources().getColor(R.color.purple_500));
        
        // 设置选中按钮样式
        switch (currentFilter) {
            case "all":
                btnFilterAll.setBackgroundResource(R.drawable.button_primary);
                btnFilterAll.setTextColor(getResources().getColor(android.R.color.white));
                break;
            case WithdrawalRecord.STATUS_PENDING:
                btnFilterPending.setBackgroundResource(R.drawable.button_primary);
                btnFilterPending.setTextColor(getResources().getColor(android.R.color.white));
                break;
            case WithdrawalRecord.STATUS_APPROVED:
                btnFilterApproved.setBackgroundResource(R.drawable.button_primary);
                btnFilterApproved.setTextColor(getResources().getColor(android.R.color.white));
                break;
            case WithdrawalRecord.STATUS_REJECTED:
                btnFilterRejected.setBackgroundResource(R.drawable.button_primary);
                btnFilterRejected.setTextColor(getResources().getColor(android.R.color.white));
                break;
        }
    }

    private void updateUIState() {
        // 如果没有记录，显示空状态
        if (adapter.getItemCount() == 0) {
            recyclerView.setVisibility(View.GONE);
            emptyState.setVisibility(View.VISIBLE);
        } else {
            recyclerView.setVisibility(View.VISIBLE);
            emptyState.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        
        // 刷新提现记录数据
        refreshData();
    }

    private void refreshData() {
        // 获取最新的提现记录
        UserInfo currentUser = mockDataManager.getCurrentUser();
        if (currentUser != null) {
            allRecords = mockDataManager.getUserWithdrawalRecords(currentUser.getUsername());
            adapter.updateData(filterRecords(currentFilter));
            updateUIState();
        }
    }
} 